package com.tuantuan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.tuantuan.dao.StudentDao;
import com.tuantuan.dao.UsersDao;
import com.tuantuan.model.Students;
import com.tuantuan.model.Users;
import com.tuantuan.service.StudentService;
import com.tuantuan.utils.GetTokenUtils;
@Service("StudentService")
@Transactional
public class StudentServiceImpl implements StudentService{

	@Resource(name="StudentDao")
	private StudentDao studentDao;
	@Resource(name="UsersDao")
	private UsersDao usersDao;
	/**
	 * 添加学生的同时添加学生用户，密码与账号同为学号
	 */
	@Override
	public String addStudent(Students s) {
		// TODO Auto-generated method stub
		Students stu = studentDao.queryStuByCode(s);
		if(stu == null){
			s.setPassword(s.getStuno());
			s.setSchoolId(1);
			Users u = new Users();
			u.setUserAccount(s.getStuno());
			u.setPassword(s.getStuno());
			u.setUserType(2);
			u.setPhone(s.getPhone());
			u.setEmail(s.getEmail());
			
			//TODO 获取token
			String token=GetTokenUtils.getToken(s.getStuno(), "",GetTokenUtils.GETTOKENURL);
			s.setToken(token);
			
			studentDao.addStudent(s);
			usersDao.addUser(u);
			return "succ";
		}
		return "fail";
	}

	/**
	 * 删除学生信息（联动删除用户信息）
	 */
	@Override
	public String deleteStuden(int stuId) {
		// TODO Auto-generated method stub
		Students s = new Students();
		s.setStuId(stuId);
		Students stu = studentDao.queryStuById(s);
		if(stu != null){
			Users u = new Users();
			u.setUserAccount(stu.getStuno());
			usersDao.deleteUser(u);
			studentDao.deleteStuden(stu);
			return "succ";
		}
		return "fail";
	}

	/**
	 * 更新学生资料（修改学号联动修改用户账号密码（重置））
	 */
	@Override
	public String updateStudent(Students s) {
		// TODO Auto-generated method stub
		Students stu = studentDao.queryStuByCode(s);
		String token="";
		if(stu == null){
			Students stuOld = studentDao.queryStuById(s);
			if(!stuOld.getStuno().equals(s.getStuno())){
				Users u1 = new Users();
				u1.setUserAccount(stuOld.getStuno());
				Users u = usersDao.queryUserByCode(u1);
				u.setUserAccount(s.getStuno());
				u.setPassword(s.getStuno());
				usersDao.updateUserBystu(u);
				
				//
				token=GetTokenUtils.getToken(s.getStuno(), "",GetTokenUtils.REFRESH);//获取新的token
				
			}else{//学号没有变
				token=stuOld.getToken();
				
			}
			studentDao.updateStudent(s);
			return "succ";
		}
		return "fail";
	}

	@Override
	public String StudentLogin(Students s) {
		Students student=studentDao.queryByStuNoAndPassword(s);
		String result="";
		String token="";
		String classId="";
		String username="";
		if(student!=null){
			//存在用户
			result="succ";
			token=student.getToken();
			classId=student.getClassId()+"";
			username=student.getStuname();
		}else{
			result="error";
		}
		
		Map<String, String> map=new HashMap<String, String>();
		map.put("result", result);
		map.put("token", token);
		map.put("classId", classId);
		map.put("username", username);
		String ret=new Gson().toJson(map);
		return ret;
	}
	
	
	public String queryStudentByClassId(String classId){
		int cId=Integer.parseInt(classId);
	 List<Students> list=studentDao.queryStudentByClassId(cId);
	 return new Gson().toJson(list);
	}
	
	public String queryStudentByName(String name){
		 List<Students> list=studentDao.queryStudentByName(name);
		 return new Gson().toJson(list);
	}
	

}
